package com.dk.logc.business;

import com.dk.logc.conf.StepType;
import com.dk.logc.config.CountFactory;
import com.dk.logc.domain.CountRole;
import com.dk.logc.domain.CountRoleSum;
import com.dk.logc.domain.FlogEntry;
import com.dk.logc.kafka.base.BaseRoleFlog;
import com.dk.logc.service.CountRoleService;
import com.dk.logc.service.CountRoleSumService;
import com.dk.logc.utils.LogicException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;


/**
 * @Author 林道敬
 * @Date 2024-01-10
 * @Description
 **/
@Component
@Slf4j
public class FlogEntryBusiness implements SuperBusiness{

    @PostConstruct
    public void init(){
        CountFactory.register("entry",this);
    }

    @Autowired
    private GameRoleBusiness gameRoleBusiness;
    @Autowired
    private CountRoleSumService countRoleSumService;
    @Autowired
    private CountRoleService countRoleService;

    @Transactional(rollbackFor = LogicException.class)
    @Override
    public void progress(BaseRoleFlog baseRoleFlog, CountRole countRole, CountRoleSum countRoleSum){
        try{
            FlogEntry flogEntry = (FlogEntry) baseRoleFlog;
            Integer step = flogEntry.getStep();
            if(step == null){
                return;
            }
            if (step.equals(StepType.ACTIVATE.getKey())) {
                countRole.setActivateNum(countRole.getActivateNum()+1);
            } else if (step.equals(StepType.REGISTER.getKey())) {   //更新注册数
                countRole.setRegisterNum(countRole.getRegisterNum()+1);
                countRoleSum.setTotalRegister(countRoleSum.getTotalRegister()+1);
            } else if (step.equals(StepType.CREATE_ROLE.getKey())) {   //更新创角数
                String tablePrefix = gameRoleBusiness.getGameRole(flogEntry) == true ? "new" : "old";
                if(tablePrefix.equals("new")){
                    countRole.setNewCreateRoleNum(countRole.getNewCreateRoleNum()+1);
                }else {
                    countRole.setOldCreateRoleNum(countRole.getOldCreateRoleNum()+1);
                }
            }
            countRoleService.saveOrUpdate(countRole);
            countRoleSumService.saveOrUpdate(countRoleSum);
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }
}
